package com.apple1.blogservices.impl;

import com.apple1.blogdao.ArticleMapper;
import com.apple1.blogdao.ArticleTagMapper;
import com.apple1.blogentity.Article;
import com.apple1.blogentity.ArticleExample;
import com.apple1.blogentity.ArticleTag;
import com.apple1.blogentity.ArticleTagExample;
import com.apple1.blogentity.vo.ArticleVo;
import com.apple1.blogservices.ArticleService;
import com.apple1.commonutil.BeanMapUtil;
import com.apple1.commonutil.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 文章服务接口的实现类
 *
 * @author cc
 *
 */

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    ArticleTagMapper articleTagMapper;

    /**
     * 方法名：添加文章
     * 处理逻辑：1.插入文章到文章表 2.插入文章包含的标签到文章标签表
     * note：none
     * @param articleVo
     * @return 成功返回非0值，代表文章插入的数量，一般来说返回1；失败返回0；
     */
    @Override
    public int addArticle(ArticleVo articleVo) {
        if (articleVo == null){
            return 0;
        }

        Article article = BeanMapUtil.getMapBean(articleVo,Article.class);
        article.setCreateDate(DateUtil.getCurrentDate());
        //插入文章
        int num =articleMapper.insertSelective(article);

        //插入文章所关联的所有标签
        List<ArticleTag> list = articleVo.getTagId();
        for(ArticleTag tagId : list){
            ArticleTag articleTag = new ArticleTag();
            articleTag.setArticleId(article.getTableId());
            articleTag.setCreateDate(DateUtil.getCurrentDate());
            articleTag.setStatus(0);
            articleTag.setTagId(tagId.getTagId());

            articleTagMapper.insertSelective(articleTag);
        }

        return num;
    }
    /**
     * 方法名:通文章的id删除文章
     * 处理逻辑: 1.id为空返回false 2.更新文章status位 3.删除文章关联的所有标签，标签的标志位status改变；
     * note(注意): 这是两个对数据库的操作是一个事务
     * @param table_id
     * @return boolean true成功；false失败；
     */
    @Override
    public boolean delArticleById(Integer table_id) {
        if (table_id == null){
            return false;
        }
        ArticleExample articleExample = new ArticleExample();

        ArticleExample.Criteria criteria = articleExample.createCriteria();
        criteria.andTableIdEqualTo(table_id);

        Article article =new Article();
        article.setStatus(1);

        //删除文章,其实就是更新
        int num = articleMapper.updateByExampleSelective(article,articleExample);
        //删除标签，更新
        ArticleTagExample articleTagExample = new ArticleTagExample();
        ArticleTagExample.Criteria criteria1 = articleTagExample.createCriteria();
        criteria1.andArticleIdEqualTo(table_id);


        ArticleTag articleTag = new ArticleTag();
        articleTag.setStatus(1);

        int num1 = articleTagMapper.updateByExampleSelective(articleTag,articleTagExample);


        return num != 0 ;
    }

    /**
     * 方法名: 根据文章id更新文章
     * 处理逻辑:1.更新文章内容，2.因为文章含有标签，所以要删除文章以前的标签 3.重新添加文章包含的标签
     * note(注意):事务
     * @param table_id
     * @param articleVo
     * @return int 0-失败，非0-成功
     */

    @Override
    public int updateArticleById(Integer table_id, ArticleVo articleVo) {
        int num=0;
        Article article = BeanMapUtil.getMapBean(articleVo,Article.class);
        if (table_id != null){
            ArticleExample articleExample = new ArticleExample();
            ArticleExample.Criteria criteria = articleExample.createCriteria();
            criteria.andTableIdEqualTo(table_id);
            num = articleMapper.updateByExampleSelective(article,articleExample);
        }

        if (table_id != null){
            //首先删除所有文章关联的标签
            ArticleTagExample articleTagExample = new ArticleTagExample();
            ArticleTagExample.Criteria criteria1 = articleTagExample.createCriteria();
            criteria1.andArticleIdEqualTo(table_id);
            articleTagMapper.deleteByExample(articleTagExample);
        }

        //更新文章的标签
        List<ArticleTag> list = articleVo.getTagId();
        for(ArticleTag tagId : list){
            ArticleTag articleTag = new ArticleTag();
            articleTag.setArticleId(article.getTableId());
            articleTag.setCreateDate(DateUtil.getCurrentDate());
            articleTag.setStatus(0);
            articleTag.setTagId(tagId.getTagId());

            articleTagMapper.insertSelective(articleTag);
        }

        return num;
    }
    /**
     * 方法名: 根据用户id查询此用户的所有文章
     * 处理逻辑:
     * note(注意):
     * @param user_id
     * @return java.util.List<com.apple1.blogentity.vo.ArticleVo>
     */
    @Override
    public List<ArticleVo> selectUserAllArticles(Integer user_id) {
        if (user_id != null) {
            ArticleExample articleExample = new ArticleExample();
            ArticleExample.Criteria criteria = articleExample.createCriteria();
            criteria.andUserIdEqualTo(user_id);

            List<Article> articles = articleMapper.selectByExample(articleExample);
            if (articles.size() > 0){
                List<ArticleVo> list = new ArrayList<>();

                for (Article article : articles){
                    ArticleVo articleVo = BeanMapUtil.getMapBean(article,ArticleVo.class);
                    ArticleTagExample articleTagExample = new ArticleTagExample();
                    ArticleTagExample.Criteria criteria1 = articleTagExample.createCriteria();
                    criteria1.andArticleIdEqualTo(articleVo.getTableId());

                    List<ArticleTag> articleTags = articleTagMapper.selectByExample(articleTagExample);
                    articleVo.setTagId(articleTags);
                    list.add(articleVo);
                }
                return list;
            }
            return null;

        }else{
           throw   new NullPointerException("查询用户文章是，user_id为空");
        }

    }


    /**
     * 方法名: 根据文章id获得文章信息
     * 处理逻辑:
     * note(注意): 文章id为空返回null
     * @param table_id
     * @return com.apple1.blogentity.Article
     */
    @Override
    public Article getArticleById(Integer table_id) {
        if (table_id == null){
            return null;
        }

        ArticleExample articleExample = new ArticleExample();
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        criteria.andTableIdEqualTo(table_id);

        List<Article> articles = articleMapper.selectByExample(articleExample);
        if (articles!=null || articles.size()>0){
            return articles.get(0);
        }
        return null;
    }
    /**
     * 方法名: 根据文章id给文章加一个赞
     * 处理逻辑:
     * note(注意): 这里有个问题，同一个用户可以重复点赞
     * @param table_id
     * @return int 0-失败 非0-成功
     */
    @Override
    public int addArticlePraiseById(Integer table_id) {
        if (table_id == null){
            return 0;
        }

        ArticleExample articleExample = new ArticleExample();
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        criteria.andTableIdEqualTo(table_id);
        Article article = null;
        List<Article> articles = articleMapper.selectByExample(articleExample);
        if (articles!=null || articles.size()>0 ){
            article = articles.get(0);
        }
        if (article != null) {
            article.setLikeNumber(article.getLikeNumber() + 1);
            article.setTableId(null);
            return  articleMapper.updateByExampleSelective(article, articleExample);
        }
        return 0;
    }


    /**
     * 方法名: 根据文章id给文章减一个赞
     * 处理逻辑:
     * note(注意):这里有个问题，同一个用户可以重复减赞
     * @param table_id
     * @return int 0-失败 非0-成功
     */
    @Override
    public int reduceArticlePraiseById(Integer table_id) {
        if (table_id == null){
            return 0;
        }

        ArticleExample articleExample = new ArticleExample();
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        criteria.andTableIdEqualTo(table_id);
        Article article = null;
        List<Article> articles = articleMapper.selectByExample(articleExample);
        if (articles!=null || articles.size()>0 ){
            article = articles.get(0);
        }
        if (article != null) {
            article.setLikeNumber(article.getLikeNumber() - 1);
            article.setTableId(null);
            return  articleMapper.updateByExampleSelective(article, articleExample);
        }
        return 0;
    }
    /**
     * 方法名: 更新文章的标签
     * 处理逻辑:  1.删除原文章的所有标签 2.添加新的文章标签
     * note(注意): 事务
     * @param article_id
     * @param tag_id
     * @return int 0-失败 非0-成功
     */
    @Override
    public int updateArticleTag(Integer article_id, Integer[] tag_id) {
        if (article_id != null){
            //首先删除所有文章关联的标签
            ArticleTagExample articleTagExample = new ArticleTagExample();
            ArticleTagExample.Criteria criteria1 = articleTagExample.createCriteria();
            criteria1.andArticleIdEqualTo(article_id);
            articleTagMapper.deleteByExample(articleTagExample);
            int counts=0;
            for (Integer i:tag_id) {
                ArticleTag articleTag = new ArticleTag();
                articleTag.setTagId(i);
                articleTag.setArticleId(article_id);
                articleTag.setCreateDate(DateUtil.getCurrentDate());
                articleTag.setStatus(0);

                int num = articleTagMapper.insertSelective(articleTag);
                counts += num;
            }
            return counts;
        }
        return 0;
    }
    /**
     * 方法名: 更新文章的分类
     * 处理逻辑:
     * note(注意):
     * @param article_id
     * @param category_id
     * @return int
     */
    @Override
    public int updateArticleCategory(Integer article_id, Integer category_id) {
        if (article_id == null || category_id == null){
            return 0;
        }

        ArticleExample articleExample = new ArticleExample();
        ArticleExample.Criteria criteria = articleExample.createCriteria();
        criteria.andTableIdEqualTo(article_id);
        Article article = null;
        List<Article> articles = articleMapper.selectByExample(articleExample);
        if (articles!=null || articles.size()>0){
            article = articles.get(0);
        }
        if (article!=null){
            article.setCategory(category_id);
            article.setTableId(null);
            return articleMapper.updateByExampleSelective(article,articleExample);
        }
        return 0;
    }


//    public static void main(String[] args){
//        ArticleVo articleVo = new ArticleVo();
//        List<Integer> list = new ArrayList<>();
//        list.add(1);
//        list.add(223);
//        articleVo.setBrowseCounts(20);
//        articleVo.setCategory(2);
//        articleVo.setContent("这里面是内容");
//        articleVo.setTitle("标题");
//        articleVo.setUserId(200);
//        articleVo.setTagId(list);
//
//        Article article = BeanMapUtil.getMapBean(articleVo,Article.class);
//        System.out.println(article);
//    }
}
